package org.geektimes.configuration.microprofile.config.source.servlet;

import org.geektimes.configuration.microprofile.config.source.MapPropertiesSource;

import javax.servlet.FilterConfig;

import java.util.Enumeration;
import java.util.Map;

import static java.lang.String.format;

/**
 * TODO
 *
 * @author karidyang
 * @since 1.0
 * Date: 2021-05-07
 **/
public class FilterConfigSource  extends MapPropertiesSource {
    private final FilterConfig filterConfig;

    public FilterConfigSource(FilterConfig filterConfig) {
        super(format("Filter[name:%s] Init Parameters", filterConfig.getFilterName()), 550);
        this.filterConfig = filterConfig;
    }

    @Override
    public void readProperties(Map properties) throws Throwable {
        Enumeration<String> parameterNames = filterConfig.getInitParameterNames();
        while (parameterNames.hasMoreElements()) {
            String parameterName = parameterNames.nextElement();
            properties.put(parameterName, filterConfig.getInitParameter(parameterName));
        }
    }
}
